//
//  SZLWebViewController.mSZLWebViewController
//  Shengzhuli
//
//  Created by 卡忙科技 on 16/7/29.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "SZLWebViewController.h"
#import "MBProgressHUD+MJ.h"
#import "PersonNetWork.h"
#import "SZLAccountHelper.h"

@interface SZLWebViewController ()<UIWebViewDelegate>

@property (nonatomic,weak) UIWebView *web;

@end

@implementation SZLWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 34, 27)];
    [backBtn setBackgroundImage:GetImage(@"btn_back_normal") forState:UIControlStateNormal];
    [backBtn setBackgroundImage:GetImage(@"btn_back_pressed") forState:UIControlStateHighlighted];
    [backBtn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    
    UIWebView *web = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH-64)];
    web.delegate = self;
    self.web = web;
    [self.view addSubview:web];
    if ((self.type == ProtocolTypeBuy)||(self.type == ProtocolTypeJoin)) {
        UIButton *sureBtn = [[UIButton alloc]initWithFrame:CGRectMake(20, ScreenH-64-60, ScreenW-40, 40)];
        [sureBtn setBackgroundImage:GetImage(@"btn_bg_login_normal") forState:UIControlStateNormal];
        [sureBtn setBackgroundImage:GetImage(@"btn_bg_login_normal") forState:UIControlStateHighlighted];
        [sureBtn setTitle:@"同意并继续" forState:UIControlStateNormal];
        [sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        sureBtn.titleLabel.font = Font(18);
        [sureBtn addTarget:self action:@selector(sureClick) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:sureBtn];
    }
    [self loadRequest];
}
- (void)sureClick
{
    if ([self isNavi]&&(self.type == ProtocolTypeJoin)) {
        [self getIsAddinfo];
    }else{
        if ([self.delegate respondsToSelector:@selector(szlWebViewControllerPop)]) {
            [self.delegate szlWebViewControllerPop];
        }
    }
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [self hideWaiter];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(nonnull NSError *)error
{
    [self hideWaiter];
    [MBProgressHUD showError:@"网络连接失败,请检查网络" toView:self.view];
    [self showRequestAgainView:@selector(loadRequest)];
}
- (void)loadRequest
{
    [self.web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.requestStr]]];
    [self hideRequestAgainView];
    [self showWaiter];
}
- (void)dismiss
{
//    if ([self isNavi]) {
//        [self.navigationController popViewControllerAnimated:YES];
//    }else{
//        [self dismissViewControllerAnimated:YES completion:nil];
//    }
    [self.navigationController popViewControllerAnimated:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (BOOL)isNavi
{
    NSArray *vcs = self.navigationController.childViewControllers;
    if (vcs.count>0) {
        if (vcs.lastObject == self){
            return YES;
        }
    }
    return NO;
}
//检查是否加盟过
- (void)getIsAddinfo
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"token"] = [SZLAccountHelper shareInstance].token;
    [PersonNetWork getAddInfoWithParame:params response:^(BOOL success, NSString *msg, SellerInfoModel *model) {
        if (success) {
            if (self.joinBlock) {
                if (!model.ID) {
                    model = [[SellerInfoModel alloc]init];
                }
                self.joinBlock(model);
            }
        }else{
            [MBProgressHUD showError:msg toView:self.view];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popToRootViewControllerAnimated:YES];
            });
        }
    }];
}

@end
